package com.stu.controller;

import com.github.pagehelper.PageHelper;
import com.stu.bean.Goods;
import com.stu.bean.Images;
import com.stu.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/getGoods")
    @ResponseBody
    public Object getAllGoods(Goods goods, @RequestParam(value = "pageSize",required = false,defaultValue = "3") int pageSize, @RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum){
        System.out.println(goods);

        System.out.println(pageSize);
        PageHelper.startPage(pageNum,pageSize);
        return goodsService.selectAllGoods(goods).toPageInfo();
    }
    @RequestMapping("/updateGoods")
    @ResponseBody
    public int updateGoods(Goods goods){
        return goodsService.updateGoods(goods);
    }

    @RequestMapping(value = "/Goods/{gid}",method = RequestMethod.DELETE)
    @ResponseBody
    public int deleteGoods(@PathVariable("gid") Integer gid){
        System.out.println(gid);
        return goodsService.delGoods(gid);
    }

    @RequestMapping(value = "/selectGoods/{gid}",method = RequestMethod.POST)
    @ResponseBody
    public Goods selectGoods(@PathVariable("gid") Integer gid){
        System.out.println(gid);
        return goodsService.selectGoods(gid);
    }

    @RequestMapping("/insertSp")
    public String goodsRegister(@Valid Goods goods, MultipartFile goodsimg, Model model, BindingResult bindingResult, HttpSession session) throws IOException {
        System.out.println(goods);

        System.out.println(goodsimg);
        if(bindingResult.hasErrors()){
            Map<String,String> map = new HashMap<String, String>();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for(FieldError fieldError:fieldErrors){
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            model.addAttribute("errors",map);
            return "spgl";
        }
        String imgPath = "c:/images/";
        String imgName = new Date().getTime()+goodsimg.getOriginalFilename();
        File file = new File(imgPath+imgName);
        goodsimg.transferTo(file);
        goods.setGimg("/images/"+imgName);

        Goods goods1 = goodsService.insertSp(goods);
        System.out.println(goods1);
        if(goods1!=null){
            session.setAttribute("insertSp",goods1);
            return "spgl";
        }else{
            return "spgl";
        }
    }
}
